package net.cmkj.mine.util;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.binary.Base64;

import net.cmkj.mine.bean.SysRole;
import net.cmkj.mine.bean.SysUser;
/**
 * 
 * @author zzcmkj
 */
public class IWebUtils {
	
	public static SysUser getCurrentSysUser(HttpSession  httpSession ) {
		SysUser sysUser = (SysUser)httpSession.getAttribute("sysUser");
		return sysUser;
	}
	
	public static void setCurrentLoginSysUserToSession(HttpSession httpSession ,SysUser sysUser){
		httpSession.setAttribute("sysUser",sysUser);
		List<SysRole> sysRoleIds = SysRole.dao.getSysRoleIdList(sysUser.getId());
		boolean isAdmin = false;
		for(SysRole sysRole : sysRoleIds){
			if(sysRole.getId().equals(1)) {
				isAdmin = true;
			}
		}
		httpSession.setAttribute("isAdmin", isAdmin);
	}
	
	public static void setCurrentLoginSysUser(HttpServletResponse response ,HttpSession httpSession,SysUser sysUser,Integer autoLogin){
		setCurrentLoginSysUserToSession(httpSession, sysUser);
		if(autoLogin.equals(1)){
			//保存30天
			long expireTime = DateUtils.addDay(new Date(),30).getTime();
			//得到用户名、经MD5加密后的用户密码、cookie有效时间
			String userInfo = MD5Utils.GetMD5Code(sysUser.getName()+sysUser.getPwd()+expireTime);
			String userBase64 = Base64.encodeBase64String(new String(sysUser.getName()+":"+expireTime+":"+userInfo).getBytes());
			CookieUtils.addCookie(response, "pm_token", userBase64, 60*60*24*30);
		}
	}
	
	/**
	 * 获取当前登陆的用户
	 * @author zzcmkj
	 * @param request
	 * @return
	 */
	public static SysUser getCurrentSysUser(HttpServletRequest request){
		SysUser sysUser=getCurrentSysUser(request.getSession());
		if(sysUser == null){
			String token = CookieUtils.getCookieByName(request, "pm_token");
			if(CommonUtils.isNotEmpty(token)){
				String[] tokenArray = new String(Base64.decodeBase64(token)).split(":");
				if(tokenArray.length == 3){
					String expireTimeStr = tokenArray[1];
					if(new Date(Long.valueOf(expireTimeStr)).after(new Date())){
						String name = tokenArray[0];
						String userInfo = tokenArray[2];
						SysUser user = SysUser.dao.getByName(name);
						if(user != null && user.getStatus().equals(1)){
							String userInfo2 = MD5Utils.GetMD5Code(user.getName()+user.getPwd()+expireTimeStr);
							if(userInfo2.equals(userInfo)){
								sysUser = user;
								setCurrentLoginSysUserToSession(request.getSession(), sysUser);
							}
						}
					}
				}
				return sysUser;
			}
		}
		return sysUser; 
	}
	
	public static void removeCurrentSysUserFromSession(HttpSession httpSession){
		httpSession.removeAttribute("sysUser");
	}
	
	public static void removeCurrentSysUser(HttpServletRequest request,HttpServletResponse response){
		CookieUtils.delCookie("", request, response);
		removeCurrentSysUserFromSession(request.getSession());
	}
	
	/*public static User getCurrentUser(HttpSession httpSession) {
		User user=(User)httpSession.getAttribute("user");
		return user;
	}*/
	
	/*public static void setCurrentLoginUserToSession(HttpSession httpSession ,User user){
		httpSession.setAttribute("user",user);
	}*/
	
	/*public static Object getCurUser(HttpSession httpSession) {
		Object usertype = httpSession.getAttribute("usertype");
		Object user = null;
		if (usertype != null) {
			if ((Integer)usertype == 1) {
				user = (Teacher)httpSession.getAttribute("user");
			} else if ((Integer)usertype == 2) {
				user = (Student)httpSession.getAttribute("user");
			}
		}
		return user;
	}*/
	
	public static void setCurrentLoginUserToSession(HttpSession httpSession ,Object user){
		httpSession.setAttribute("user",user);
	}
	
	public static void removeCurrentUserFromSession(HttpSession httpSession){
		httpSession.removeAttribute("user");
	}
	
	public static void removeCurrentUser(HttpServletRequest request,HttpServletResponse response){
		CookieUtils.delCookie("", request, response);
		removeCurrentUserFromSession(request.getSession());
	}
}
